package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.mapper.DeptMapper;
import com.usian.mapper.EmpMapper;
import com.usian.mapper.ZhiWuMapper;
import com.usian.pojo.Dept;
import com.usian.pojo.Emp;
import com.usian.pojo.EmpVo;
import com.usian.pojo.Zhiwu;
import com.usian.service.EmpService;
import javafx.concurrent.Worker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.List;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private ZhiWuMapper zhiWuMapper;
    @Override
    public List<EmpVo> findAll(Integer type,Integer did) {
        return empMapper.findAll(type,did);
    }

    @Override
    public void add(Emp emp) {
        empMapper.insert(emp);
    }

    @Override
    public void edit(Emp emp) {
        empMapper.updateById(emp);
    }

    @Override
    public List<Zhiwu> findZid() {
        return zhiWuMapper.selectList(null);
    }

    @Override
    public List<Dept> findDid() {
        return deptMapper.selectList(null);
    }

    @Override
    public Emp findById(Integer id) {
        return empMapper.selectById(id);
    }

    @Override
    public Emp login(Emp emp) {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.eq("name",emp.getName());
        wrapper.eq("pwd",emp.getPwd());
        return empMapper.selectOne(wrapper);
    }

    @Override
    public void delete(Integer id) {
        empMapper.deleteById(id);
    }

    @Override
    public void addDept(Dept dept) {
        deptMapper.insert(dept);
    }

    @Override
    public void addZhiWu(Zhiwu zhiwu) {
        zhiWuMapper.insert(zhiwu);
    }

    @Override
    public void deleteDept(Integer did) {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.eq("did",did);
        List<Emp> empList = empMapper.selectList(wrapper);
        if (empList.size()==0){
            QueryWrapper<Zhiwu> zwrapper = new QueryWrapper<>();
            zwrapper.eq("did",did);
            zhiWuMapper.delete(zwrapper);
            deptMapper.deleteById(did);
        }
    }
}
